package com.cyandpos.pad.pay.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.DialogConstant;
import com.cyandpos.pad.common.commondata.OnDialogListener;
import com.cyandpos.pad.posdata.CashCardItem;
import com.cyandpos.pad.sale.saleutils.SaleUtil;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zx on 2017/3/6.
 */

public class PayCashCardListAdapter extends BaseAdapter {
    private Context context;
    private List<CashCardItem> cashCardItems = new ArrayList<CashCardItem>();
    private Handler handler;
    private int index=-1;
    public PayCashCardListAdapter(Context context, List<CashCardItem> cashCardItems, Handler handler) {
        this.context = context;
        this.cashCardItems = cashCardItems;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return cashCardItems.size();
    }

    @Override
    public Object getItem(int position) {
        return cashCardItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        Holder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.pay_cashcardlist_item, null );
            holder=new Holder();
            holder.ll_common=convertView.findViewById(R.id.ll_common);
            holder.tv_name = (TextView) convertView.findViewById(R.id.pay_name);
            holder.tv_money = (TextView) convertView.findViewById(R.id.pay_name_money);
            holder.delete = (ImageButton) convertView.findViewById(R.id.ib_delete);
            convertView.setTag(holder);
        }else{
            holder= (Holder) convertView.getTag();
        }
        if(index==position){
            holder. ll_common.setBackgroundResource(R.color.color_b6);
        }else{
            holder.ll_common.setBackgroundResource(R.color.white);
        }
        holder.tv_name.setText("储值卡("+cashCardItems.get(position).getCardNo()+")");
        holder.tv_money.setText(MoneyUtils.getMoneyStr(cashCardItems.get(position).getmMoney())+"");
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SaleUtil.showInfoDialog(context, "注意", "是否删除", new OnDialogListener() {
                    @Override
                    public void onClick(int flag) {
                        if(flag== DialogConstant.DIALOG_BTN_SURE) {
                            Message msg = new Message();
                            msg.what = 0x00;
                            msg.obj = (String) cashCardItems.get(position).getCardNo();
                            handler.sendMessage(msg);
                        }
                    }
                });
            }
        });


        return convertView;
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    class Holder{
       private  LinearLayout ll_common;
        private TextView tv_name;
        private TextView tv_money;
        private ImageButton delete;
    }
}
